package com.quyunshuo.module.home.room

import android.content.Context
import android.util.Log
import com.quyunshuo.module.home.room.database.SumDataBase
import com.quyunshuo.module.home.room.entity.PoetEntity
import com.quyunshuo.module.home.room.manager.PoetRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch

object DataInitializer {
    fun initializeDatabase(context: Context) {
         val videoDao by lazy { SumDataBase.getInstance().poetDao() }

        val repository = PoetRepository(videoDao)
        
        CoroutineScope(Dispatchers.IO).launch {
            // 检查数据库是否已有数据
            val count = repository.allPoets.firstOrNull()?.size ?: 0
            if (count == 0)
                // 插入初始数据
                repository.insertAll(getInitialPoets())
        }
    }

    private fun getInitialPoets(): List<PoetEntity> {
        return listOf(
            PoetEntity(
                id = 1,
                name = "李白",
                lifespan = "61岁",
                intro = "唐代伟大的浪漫主义诗人，被后人誉为'诗仙'，与杜甫并称'李杜'。诗风豪放飘逸，想象丰富瑰丽，语言清新明快。",
                style = "豪放飘逸，想象奇特",
                birthplace = "唐安西都护府碎叶城（今吉尔吉斯斯坦托克马克）",
                portraitResource = "libai"
            ),
            PoetEntity(
                id = 2,
                name = "杜甫",
                lifespan = "58岁",
                intro = "唐代伟大的现实主义诗人，有'诗圣'之称。其诗反映唐朝由盛转衰的历史过程，被称为'诗史'。",
                style = "沉郁顿挫，写实深沉",
                birthplace = "河南巩县（今河南巩义）",
                portraitResource = "dufu"
            ),
            // 添加其他诗人数据...
            PoetEntity(
                id = 11,
                name = "曹植",
                lifespan = "40岁",
                intro = "三国时期著名文学家，建安文学的代表人物与集大成者。后人因他文学上的造诣而与曹操、曹丕合称'三曹'。",
                style = "词采华茂，慷慨悲凉",
                birthplace = "沛国谯县（今安徽省亳州市）",
                portraitResource = "caozhi"
            )
        )
    }
}